package com.yhzm.yhserver.generator;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yhzm.yhserver.Utils.GeneralUtil;
import com.yhzm.yhserver.YhserverbootApplication;
import com.yhzm.yhserver.bean.D_00001;
import com.yhzm.yhserver.bean.J_00001;
import com.yhzm.yhserver.bean.J_00002;
import com.yhzm.yhserver.dao.D_00001Mapper;
import com.yhzm.yhserver.dao.J_00001Mapper;
import com.yhzm.yhserver.dao.J_00002Mapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {YhserverbootApplication.class})// 指定启动类
public class CreateD_00001 {

    @Autowired
    private D_00001Mapper d_00001Mapper;

    @Autowired
    private J_00001Mapper j_00001Mapper;

    @Autowired
    private J_00002Mapper j_00002Mapper;


    @Test
    public void insert() {
        QueryWrapper<D_00001> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.orderByAsc("field1");
        List<D_00001> d_00001s = d_00001Mapper.selectList(objectQueryWrapper);
        for (int i = 3595; i <= 3626; i++) {
            Long flag = 101000000000L + i;
            for (D_00001 d_00001 : d_00001s) {
                if (d_00001.getField1().equals(flag.toString())) {
                    d_00001s.remove(d_00001);
                    break;
                } else {
                    try {
                        String key = GeneralUtil.generateCode("1001700000");
                        d_00001.setCode(key);
                        d_00001.setField1(flag.toString());
                        d_00001.setField2("");
                        d_00001.setField4("");
                        d_00001.setField6("");
                        d_00001.setInputtime(null);
                        d_00001.setField100(null);
                        d_00001.setIsvalid(null);
                        //System.out.println(d_00001.getField1());
                        d_00001Mapper.insert(d_00001);
                    } catch (DuplicateKeyException e) {
                        insert();
                        System.out.println("重复键");
                    }
                    break;
                }
            }
        }
    }

    @Test
    public void insert2() {
        String key = GeneralUtil.generateCode("1001700000");
        for (int i = 0; i < 1; i++) {
            Long flag = 100000000000L + i;
            D_00001 d_00001 = new D_00001();
            d_00001.setCode(key);
            d_00001.setField1(flag.toString());
            d_00001.setField2("100000000055");
            d_00001.setField4("");
            d_00001.setField6("");
            d_00001.setField17(1);
            d_00001.setField18(0);
            d_00001.setInputtime(null);
            d_00001.setField100(null);
            d_00001.setIsvalid(null);
            d_00001Mapper.insert(d_00001);
        }
    }

    @Test
    public void test2() {
        Map<String, J_00001> j_00001Map = new HashMap<>();
        Map<String, J_00002> j_00002Map = new HashMap<>();
        List<J_00001> j_00001s = j_00001Mapper.selectList(new QueryWrapper<>());
        List<J_00002> j_00002s = j_00002Mapper.selectList(new QueryWrapper<>());
        List<D_00001> d_00001s = d_00001Mapper.selectList(new QueryWrapper<>());
        //赋值Map
        for (J_00001 j_00001 : j_00001s) {
            j_00001Map.put(j_00001.getCode(), j_00001);
        }
        for (J_00002 j_00002 : j_00002s) {
            j_00002Map.put(j_00002.getCode(), j_00002);
        }

        d_00001s.forEach(d_00001 -> {
            J_00001 j_00001 = j_00001Map.get(d_00001.getField4());
            J_00002 j_00002 = j_00002Map.get(d_00001.getField6());
            //非空判断
            if (j_00002 != null) {
                j_00002.setField21(j_00001.getCode());
                j_00002.setField22(j_00001.getField5());
                j_00002.setField12(j_00002.getField10());
                j_00002.setField13(j_00002.getField11());
                j_00002.setField9("");
                j_00002.setField10("");
                j_00002.setField11("");
            }
        });
        j_00002s.forEach(j_00002 -> {
            System.out.println(
                    "field1：" +
                            j_00002.getField1() + " field10："
                            + j_00002.getField10() + " field11： "
                            + j_00002.getField11() + " field12： "
                            + j_00002.getField12() + " field13： "
                            + j_00002.getField13() + " field14： "
                            + j_00002.getField14() + " field21： "
                            + j_00002.getField21() + " field22： "
                            + j_00002.getField22() + ""
            );
        });
    }

    @Test
    public void test3() {
        int i = 1;
        do {
            String iStr = i + "";
            if (i % 7 == 0 || iStr.indexOf("7") != -1) {

            } else {
                System.out.println(iStr);
            }
            i++;
        } while (i <= 100);
    }

    /*@Test
    public void test() {
        for (int i = 1; i <= 300; i++) {
            J_00002 j_00002 = new J_00002();
            j_00002.setCode(GeneralUtil.generateCode("1000900000"));
            String j_00002Field1 = "";
            if (i < 10) {
                j_00002Field1 += "水寨大道00" + i + "杆";
            } else if (i < 100) {
                j_00002Field1 += "水寨大道0" + i + "杆";
            } else if (i<1000){
                j_00002Field1 += "水寨大道" + i + "杆";
            }
            j_00002.setField1(j_00002Field1);
            j_00002.setField2(30D);
            j_00002.setField3(new Date());
            j_00002.setField4(new Date());
            j_00002.setField5("五华路灯所");
            j_00002.setField9("水寨大道1号箱");
            j_00002.setField10("广东省");
            j_00002.setField11("梅州市");
            j_00002.setField12("五华县");
            j_00002.setField13("水寨镇");
            j_00002.setField14("水寨大道");
            System.out.println(j_00002.getField1());
            j_00002Mapper.insert(j_00002);
        }

    }*/
}
